local extension = Package:new("love_diy")
local U = require "packages/utility/utility"
local lib = require "packages/love/lib"
extension.extensionName = "love"


local busi = General(extension, "love__busi", "qun", 4)

local jusha = fk.CreateTriggerSkill{
    name = "jusha",
    events = {fk.CardUseFinished},
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and target == player and player.phase == Player.Play and data.card.suit ~= Card.NoSuit
        and U.IsUsingHandcard(player, data) then
            local card_suit = data.card.suit
            local room = player.room
            local logic = room.logic
            local current_event = logic:getCurrentEvent()
            local mark_name = "jusha_" .. data.card:getSuitString() .. "-phase"
            local mark = player:getMark(mark_name)
            if mark == 0 then
                logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                    local use = e.data[1]
                    if use.from == player.id and use.card.suit == card_suit and lib.IsUsingHandcard(player,e) then
                        mark = e.id
                        room:setPlayerMark(player, mark_name, mark)
                        return true
                    end
                end, Player.HistoryPhase)
            end
            return mark == current_event.id
        end
        
        return false
    end,
    on_trigger = function(self, event, target, player, data)
        local mark = U.getMark(player,"@jusha-phase")
        table.insertIfNeed(mark, data.card:getSuitString(true))
        player.room:setPlayerMark(player, "@jusha-phase", mark)
        self:doCost(event, target, player, data)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cards = room:getNCards(3+player:getMark("@jusha-turn"))
        room:setPlayerMark(player,"@jusha-turn",0)
        room:moveCards({
            ids = cards,
            toArea = Card.Processing,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            proposer = player.id,
        })
        room:delay(500)
        --统计花色
        local suits = {}
        for _, id in ipairs(cards) do
            local  card = Fk:getCardById(id)
            table.insertIfNeed(suits,card.suit)
        end
        local x= player:getHandcardNum()
        local n = player:getHandcardNum() - #suits
        if n > 0 then
            local dis =room:askForDiscard(player, n, n, false, self.name, false)
            if #dis > 0 then
                --没有使用牌
                local flag = true
                while(true)
                do
                    local use =  U.askForUseRealCard(room, player, cards, ".", self.name, "#jusha-use", {expand_pile = cards,bypass_distances=true,bypass_times=true}, true, true)
                    if use then
                        use.extraUse = true
                        room:useCard(use)
                        flag = false
                    else
                        break
                    end
                    cards = table.filter(cards,function(id) return room:getCardArea(id) == Card.Processing end)
                end
                if  flag then
                    room:setPlayerMark(player,"@jusha-turn",2)
                end

            end
        elseif n <0 then
            player:drawCards(-n, self.name)
        end
        cards = table.reverse(cards)
        room:moveCards({
            ids = cards,
            from = player.id,
            toArea = Card.DrawPile,
            moveReason = fk.ReasonPut,
            skillName = self.name,
            moveVisible = true,
        })

    end,

}
busi:addSkill(jusha)
Fk:loadTranslationTable{
    ["love__busi"] ="卜巳",
    ["jusha"] ="聚煞",
    [":jusha"]="当你于出牌阶段首次使用一种花色的手牌后。可以展示牌堆顶三张牌并将手牌调整至展示牌的花色数，若你以此法弃牌则你可使用任意张展示的牌(无距离次数限制)或使本回合下一次发动聚煞多展示两张牌。",
    ["#jusha-use"] = "聚煞",
    ["@jusha-phase"] = "聚煞",
    ["@jusha-turn"] = "聚煞"

}

local maohuanghou = General(extension, "maohuanghou", "wei", 3)

local shichong = fk.CreateTriggerSkill{
    name = "love_shichong",
    switch_skill_name = "love_shichong",
    events = {fk.BeforeDrawCard},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
       return player:hasSkill(self) and target == player
    end,
    on_use = function(self, event, target, player, data)
        if player:getSwitchSkillState(self.name) == fk.SwitchYang then
            data.num = 3
        else
            data.num = 1
        end
    end
}


local panjun = fk.CreateTriggerSkill{
    name = "love_panjun",
    events = {fk.DamageCaused},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
            local x = math.abs(target:getHandcardNum() - data.to:getHandcardNum())
            return player:hasSkill(self) and (x == 0 or x >= player.hp) and data.to == player
    end,
    on_use = function(self, event, target, player, data)
        local x = math.abs(target:getHandcardNum() - data.to:getHandcardNum())
        if x == 0  then
            data.damage = data.damage + 1
        else
            data.damage = data.damage - 1
        end
    end
}

local chuyan_ac = fk.CreateActiveSkill{
    name = "chuyan_ac",
    interaction = function()
        return UI.ComboBox{ choices = {"Discard", "Recast"} }
    end,
    card_filter = function(self, to_select, selected)
        if  self.interaction.data == "Discard" then
            return not Self:prohibitDiscard(Fk:getCardById(to_select))
        else
            return true
        end
    end,
    target_filter = function(self, to_select, selected)
        return false
    end,
    target_num = 0,
    card_num = 1,
}
Fk:addSkill(chuyan_ac)

local chuyan = fk.CreateTriggerSkill{
    name = "chuyan",
    events = {fk.TargetConfirmed},
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if target == player  and player:hasSkill(self) then
            local from = room:getPlayerById(data.from)

            if from:isNude() or player:isNude() then return false end
            local mark = player:getMark("chuyan-turn")
            local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
            if use_event == nil then return false end
            if mark == 0 then
                room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                    local use = e.data[1]
                    if table.contains(TargetGroup:getRealTargets(use.tos), player.id)  and use.from ~= player.id then
                        mark = e.id
                        room:setPlayerMark(player, "chuyan-Turn", mark)
                        return true
                    end
                end, Player.HistoryTurn)
            end
            return mark == use_event.id
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local  players = {player.id,data.from}
        room:sortPlayersByAction(players)
        local results = {}
        for _, id in ipairs(players) do
            local a,result =room:askForUseActiveSkill(room:getPlayerById(id), "chuyan_ac", "#chuyan", false)
            table.insert(results,result)
        end

        for i, id in ipairs(players) do
            local result = results[i]
            if result.interaction == "Discard" then
                room:throwCard(result.cards,self.name,room:getPlayerById(id))
            else
                room:recastCard(result.cards, room:getPlayerById(id), self.name)
            end
        end
    end
}

maohuanghou:addSkill(shichong)
maohuanghou:addSkill(panjun)
maohuanghou:addSkill(chuyan)

Fk:loadTranslationTable{
    ["maohuanghou"] ="毛皇后",
    ["love_shichong"] ="时宠",
    [":love_shichong"]="锁定技，转换技，当你摸牌时阳:改为摸三张牌：阴:改为摸一张牌。",
    ["love_panjun"] = "畔君",
    [":love_panjun"] = "锁定技，与你手牌数之差大于等于你体力值的角色对你造成的伤害-1，手牌数等于你的角色对你造成的伤害+1。",
    ["chuyan"] = "触颜",
    [":chuyan"] = "当你每回合首次成为其他角色使用牌的目标后，你可以与其同时选择重铸或弃置一张牌。",
    ["chuyan_ac"] = "触颜",
    ["Discard"]= "弃置",
    ["Recast"]= "重铸",
    ["#chuyan"] = "触颜:弃置或重铸一张牌"
}

return extension